<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
	private $_config;
	/*get a copy of the config*/
	public function _initBootstrap(){
		$this->_config = Yaf_Application::app()->getConfig();
	}
	/*
     * initIncludePath is only required because zend components have a shit load of
     * include_once calls everywhere. Other libraries could probably just use
     * the autoloader (see _initNamespaces below).
     */
	public function _initIncludePath(){
		set_include_path(get_include_path().PATH_SEPARATOR.$this->_config->application->library);
	}
	public function _initErrors(){
		if ($this->_config->application->showErrors){
			error_reporting(-1);
			ini_set('display_errors', 'On');
		}
	}
	public function _initNamespaces(){
		Yaf_Loader::getInstance()->registerLocalNameSpace(array("Zend"));
	}
	public function _initRoutes(){
		//this does nothing useful but shows the regex router in action...
		Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
			"paging_example",
			new Yaf_Route_Regex(
				"#^/index/page/(\d+)#",
				array('controller'=>"index"),
				array(1=>"page")
			)
		);
	}

		public function _initMailer(Yaf_Dispatcher $dispatcher) {
		if (isset($this->_config->smtp))
		{
			if (isset($this->_config->smtp->server))
			{
				define('SMTP_SERVER', $this->_config->smtp->server);
			}
			if (isset($this->_config->smtp->ssl))
			{
				define('SMTP_SSL', $this->_config->smtp->ssl);
			}
			if (isset($this->_config->smtp->username))
			{
				define('SMTP_USERNAME', $this->_config->smtp->username);
			}
			if (isset($this->_config->smtp->password))
			{
				define('SMTP_PASSWORD', $this->_config->smtp->password);
			}
			if (isset($this->_config->smtp->helo))
			{
				define('SMTP_HELO', $this->_config->smtp->helo);
			}
			Yaf_Loader::import(APP_PATH . '../library/PHPMailer/PHPMailer.php');
		}
	}

	public function _initLayout(Yaf_Dispatcher $dispatcher){//注册插件
		/*layout allows boilerplate HTML to live in /views/layout rather than every script*/
		$layout = new LayoutPlugin('layout.phtml');
		 /* Store a reference in the registry so values can be set later.
         * This is a hack to make up for the lack of a getPlugin
         * method in the dispatcher.
         */
		Yaf_Registry::set('layout',$layout);
		/*add the plugin to the dispatcher*/
		$dispatcher->registerPlugin($layout);
	}

	public function _initView(Yaf_Dispatcher $dispatcher) {
        $view= new Smarty_Adapter(null, $this->_config->smarty);
        Yaf_Dispatcher::getInstance()->setView($view);
    }
	public function _initDefaultDbAdapter(){
		$dbAdapter = new Zend_Db_Adapter_Pdo_Mysql(
			$this->_config->database->params->toArray()
		);
		$dbAdapter->query("set names 'utf8'");
		Zend_Db_Table::setDefaultAdapter($dbAdapter);
	}
}